package com.sky.controller.admin;

import com.sky.dto.OrdersCancelDTO;
import com.sky.dto.OrdersConfirmDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.vo.OrderStatisticsVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController(value = "adminOrderController")
@RequestMapping(value = "/admin/order")
@Slf4j
@Api(tags = "订单管理相关接口")
public class OrderController {
    @Autowired
    private com.sky.service.OrderService orderService;

    /*商家条件搜索订单*/
    @GetMapping(value = "/conditionSearch")
    @ApiOperation(value = "订单搜索方法")
    public Result<PageResult> conditionSearchOrders(OrdersPageQueryDTO ordersPageQueryDTO) {
        // 记录日志
        log.info("订单搜索方法，参数为{}", ordersPageQueryDTO);
        // 调用业务层
        PageResult pageResult = orderService.conditionSearchOrders(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /*统计各类型订单数量，GET类型请求，没有请求参数*/
    @GetMapping(value = "/statistics")
    @ApiOperation(value = "各类型订单数量统计方法")
    public Result<OrderStatisticsVO> statistics() {
        // 记录日志
        log.info("统计各类型订单数量方法");
        // 调用业务层
        OrderStatisticsVO orderStatisticsVO = orderService.statistics();
        return Result.success(orderStatisticsVO);
    }

    /*查看订单详情方法*/
    @GetMapping(value = "/details/{id}")
    @ApiOperation(value = "查看订单详情方法")
    public Result<OrderVO> getDetailByOrderId(@PathVariable(value = "id") Long id) {
        // 记录日志
        log.info("查看订单详情方法，参数为{}", id);
        // 调用业务层
        OrderVO orderVO = orderService.getDetailByOrderId(id);
        return Result.success(orderVO);
    }

    /*商家接单（用户付款以后的操作），本质就是修改订单的状态，其他的都不用变*/
    @PutMapping(value = "/confirm")
    @ApiOperation(value = "商家接单方法")
    public Result<String> confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO) {
        // 记录日志
        log.info("商家接单方法，参数为{}", ordersConfirmDTO);
        // 调用业务层
        orderService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    /*商家拒绝订单方法（必须是待接单的状态才可以拒单）*/
    @PutMapping(value = "/rejection")
    @ApiOperation(value = "商家拒单方法")
    public Result<String> rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) {
        /*记录日志*/
        log.info("商家拒单方法，参数为{}", ordersRejectionDTO);
        /*调用业务层*/
        orderService.rejection(ordersRejectionDTO);
        return Result.success();
    }

    /*商家取消订单，此时订单状态必须是待接单*/
    @PutMapping(value = "/cancel")
    @ApiOperation(value = "商家取消订单方法")
    public Result<String> cancelOrder(@RequestBody OrdersCancelDTO ordersCancelDTO) {
        // 记录日志
        log.info("商家取消订单方法，参数为{}", ordersCancelDTO);
        // 调用业务层
        orderService.cancelOrder(ordersCancelDTO);
        return Result.success();
    }

    /*商家订单配送方法，只有状态为待配送的订单才可用，否则报异常*/
    @PutMapping(value = "/delivery/{id}")
    @ApiOperation(value = "商家订单配送方法")
    public Result<String> deliveryOrder(@PathVariable(value = "id") Long id) {
        // 记录日志
        log.info("商家订单配送方法，参数为订单id：{}", id);
        // 调用业务层
        orderService.deliveryOrder(id);
        return Result.success();
    }

    /*商家完成订单方法，只有订单状态为派送中的订单才能完成订单*/
    @PutMapping(value = "/complete/{id}")
    @ApiOperation(value = "商家订单完成方法")
    public Result<String> completeOrder(@PathVariable(value = "id") Long id) {
        // 记录日志
        log.info("商家订单完成方法，参数为订单id：{}", id);
        // 调用业务层
        orderService.completeOrder(id);
        return Result.success();
    }

}
